<?php
use Managers\Auto,
    Managers\Search,
    Managers\Ad;

class UserController extends Zend_Controller_Action
{
    public function init()
    {
        $this->_helper->layout()->setLayout('user-layout');
    }

    public function indexAction()
    {
        
    }

    public function deleteipspamerAction()
    {
        $id_spamer=$this->_getParam('id_spamer');
        $ip_spamer=$this->_getParam('ip');
        $spamerManager = new Managers\Users();
        $delIpSpam = $spamerManager->DeleteIpSpamer($id_spamer);
        $message = "IP-адресс \"".$ip_spamer."\" удален со списка спамеров!";
        $this->_redirect(\Zend_Registry::get('config')->resources->frontController->baseUrl.'user/spamer/message/'.$message);
    }

    public function deletemailspamerAction()
    {
        $id_spamer=$this->_getParam('id_spamer');
        $mail_spamer=$this->_getParam('mail');
        $spamerManager = new Managers\Users();
        $delIpSpam = $spamerManager->DeleteMailSpamer($id_spamer);
        $message = "E-mail \"".$mail_spamer."\" удален со списка спамеров!";
        $this->_redirect(\Zend_Registry::get('config')->resources->frontController->baseUrl.'user/spamer/message/'.$message);
    }

    public function viewAction()
    {
        $error = $this->_getParam('error');
        if ($error==1)
            $this->view->ErrorMessageInLogPas = "Error in the username or (and) password";
        $exit = $this->_getParam('exit');
        if ($exit==1)
        {
            $session = new Zend_Session_Namespace('default');
            $session->user_info = 0;
            $this->_redirect(\Zend_Registry::get('config')->resources->frontController->baseUrl.'user');
        }
    }

    public function loginAction()
    {

        $login = $this->_getParam('login_name');
        $pas = $this->_getParam('pas');

        $UserLogin = new Managers\Users();
        $user_info = $UserLogin->IsAdmin($login, $pas);
        if ($user_info==false)
        {
            $this->_redirect(\Zend_Registry::get('config')->resources->frontController->baseUrl.'user/view/error/1');
        }
        else
        {
            $session = new Zend_Session_Namespace('default');
            $session->user_info = $user_info;
            $this->_redirect(\Zend_Registry::get('config')->resources->frontController->baseUrl.'user/spamer');
        }
    }

    public function spamerAction()
    {
        $session = new Zend_Session_Namespace('default');
        $allUserInfo = $session->user_info; //достали інфу про юзера
        if ($allUserInfo['admin']==0)
            $this->_redirect(\Zend_Registry::get('config')->resources->frontController->baseUrl.'user');

        if ($this->_getParam('message'))
            $this->view->message = $this->_getParam('message');
        $this->_helper->layout()->setLayout('spamer-layout');

        $notSpamer = $this->_getParam('delete_id');
        $addSpamer = 0;

        $userManager = new Managers\Users();
        $this->view->IPspamList = $userManager->IPSpamList();
        $this->view->MailSpamList = $userManager->MailSpamList();
    }

    public function infoAction()

    {
 $this->_helper->layout()->setLayout('index-layout');
       $userManager = new \Managers\Users();
       $this->view->userinfo = $userManager->infoShow($this->_getParam('user'));


        $array = $userManager->infoShow($this->_getParam('user'));

        //      $curpage=1;  //potochna storinka
        $pages = 10; //kilkist  storinok sho vuvodyatsya max
        $data = 3; //kilkist elementiv na storinci

        $pageCount = count($array);
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($this->view->userinfo ));

        $paginator->setCurrentPageNumber($this->_getParam('page'));
        $paginator->setPageRange($pageCount);
        $paginator->setItemCountPerPage($data);

        $this->view->paginator = $paginator;

    }
       public  function resetpasswordAction(){
//        $mail=new Managers\Mail();

            $sdb=new \Managers\Security();
            $udb=new \Managers\Users();
            $new_pas=$sdb->generate_password(16);
            $user_id=1;

//        $text="Hello , you hav a new message <br>Your new password are ".$new_pas;
            $udb->setPas($user_id,$new_pas);

            print_r($new_pas);
            die;

//        $mail->sendMail($text,
//                        "password reseter",
//                        "admin@mai.mysite.ru",
//                        "admin my site",
//                        "nazymko.patronus@gmail.com",
//                        "for you");



    }


}
